package com.example.mysdktest.activities;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.mysdktest.R;
import com.example.mysdktest.base.BaseActivity;
import com.xm9m.ibczy.dsp.https.RequestQueue;
import com.xm9m.ibczy.dsp.https.http.BCGetRequest;
import com.xm9m.ibczy.dsp.https.http.BCHttpListener;


/**
 * Created by baichuan on 2016/11/25.
 */

public class MyHttpTest01 extends BaseActivity implements View.OnClickListener {
    // 请求的URL地址
    public static final String url = "http://10.1.1.192:8080/";
    public static final String url2 = "https://www.baidu.com/";
    public static final String imgUrl1 = "http://cdn.dsp.ibczy.com/adImages/7/f9yrKZ8lozVzVKvj3D0VRhgCeMI5t3.jpg";
    public static final String imgUrl2 = "http://pic.pp3.cn/uploads//allimg/111125/16030S140-0.jpg";

    private Button obt;
    private TextView text;
    private ImageView image;
    private RequestQueue queue;


    @Override
    public int getLaout() {
        queue = new RequestQueue.RequestQueueBuilder().builder();
        return R.layout.ac_http_test01;
    }

    @Override
    public void initView() {
        obt = (Button) findViewById(R.id.ac_http_test01_obt01);
        text = (TextView) findViewById(R.id.ac_http_test01_otv01);
        image = (ImageView) findViewById(R.id.ac_http_test01_img01);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {
        obt.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ac_http_test01_obt01:

                try {
                    // getData();
                    getData1();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
        }
    }

    public void getData1() throws Exception {

        // http get request
        BCGetRequest ree = new BCGetRequest(url2).withListener(new BCHttpListener() {
            @Override
            public void onSuccess(int statueCode, byte[] data) {
                final String ss = new String(data);
                text.post(new Runnable() {
                    @Override
                    public void run() {
                        text.setText(new String(ss));
                    }
                });
            }

            @Override
            public void onError(int statueCode, String message) {

            }
        });

        BCGetRequest rimg = new BCGetRequest(imgUrl2).withListener(new BCHttpListener() {
            @Override
            public void onSuccess(int statueCode, byte[] data) {
                final Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                if (bitmap != null)
                    image.post(new Runnable() {
                        @Override
                        public void run() {
                            image.setImageBitmap(bitmap);
                        }
                    });
            }

            @Override
            public void onError(int statueCode, String message) {

            }
        });

        queue.add(ree);
        queue.add(rimg);
    }
}
